/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessBillListForOutputExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessBillListForOutput.xls\"");
		HSSFSheet sheet = workbook.createSheet("list");
		List list = (List) model.get("list");
		List mtList = (List) model.get("mtList");
		List wcList = (List) model.get("wcList");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		c.setCellValue(new HSSFRichTextString("制作项目：" + list.size()));
		sheet.addMergedRegion(new Region(0, 0, 0, 11));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("日期"));
		r.createCell(1).setCellValue(new HSSFRichTextString("单号"));
		r.createCell(2).setCellValue(new HSSFRichTextString("订货单位"));
		r.createCell(3).setCellValue(new HSSFRichTextString("项目类型"));
		r.createCell(4).setCellValue(new HSSFRichTextString("内容"));
		r.createCell(5).setCellValue(new HSSFRichTextString("材质"));
		r.createCell(6).setCellValue(new HSSFRichTextString("规格"));
		r.createCell(7).setCellValue(new HSSFRichTextString("单位"));
		r.createCell(8).setCellValue(new HSSFRichTextString("数量"));
		r.createCell(9).setCellValue(new HSSFRichTextString("制作要求"));
		r.createCell(10).setCellValue(new HSSFRichTextString("工作中心"));
		r.createCell(11).setCellValue(new HSSFRichTextString("完成情况"));
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			BusinessBillItem b = (BusinessBillItem) objs[0];
			String spe = b.getSpecification();
			if (b.getPriceType().equals("AREA")
					|| b.getPriceType().equals("LENGTH")) {
				spe = spe + " x "
						+ EncCommonHelper.parseBigDecimal(b.getAmount());
			}

			BigDecimal amount;
			if (!b.getPriceType().equals("AREA")
					&& !b.getPriceType().equals("LENGTH")) {
				amount = b.getAmount();
			} else {
				amount = b.getSquareNum();
			}

			String estName = "";
			if (!EncCommonHelper.checkPurePrivilege(request, "NO_CLIE_NAME")) {
				estName = (String) objs[3];
				if (objs[4] != null && !objs[4].toString().equals("")) {
					estName = estName + "(" + objs[4] + ")";
				}
			}

			String unit = "";
			if (b.getPriceType().equals("AREA")) {
				unit = "平方米";
			} else if (b.getPriceType().equals("LENGTH")) {
				if (b.getLengType().equals("M")) {
					unit = "米";
				} else {
					unit = "公分";
				}
			} else if (b.getPriceType().equals("AMOUNT")) {
				unit = b.getUnit();
			}

			String compStr = (String) objs[5];
			r = sheet.createRow(os + 2);
			r.createCell(0).setCellValue(
					new HSSFRichTextString(sf.format((Date) objs[1])));
			r.createCell(1).setCellValue(
					new HSSFRichTextString((String) objs[2]));
			r.createCell(2).setCellValue(new HSSFRichTextString(estName));
			r.createCell(3).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							mtList, b.getMakeTypeId())));
			r.createCell(4).setCellValue(
					new HSSFRichTextString(b.getFinishedContent()));
			r.createCell(5)
					.setCellValue(new HSSFRichTextString(b.getProName()));
			r.createCell(6).setCellValue(new HSSFRichTextString(spe));
			r.createCell(7).setCellValue(new HSSFRichTextString(unit));
			r.createCell(8).setCellValue(amount.doubleValue());
			r.createCell(9).setCellValue(
					new HSSFRichTextString(b.getMakeRequire()));
			r.createCell(10).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							wcList, b.getWcId())));
			r.createCell(11).setCellValue(new HSSFRichTextString(compStr));
		}

		ServletOutputStream arg19 = response.getOutputStream();
		workbook.write(arg19);
		arg19.flush();
		arg19.close();
	}
}